package org.wingstudio.controller;


import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.wingstudio.entity.PageBean;
import org.wingstudio.service.FileService;
import org.wingstudio.util.PageUtil;
import org.wingstudio.util.StringUtil;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游客预览文件，下载文件模块
 * Created by liao on 16-11-19.
 */

@Controller
@RequestMapping("/file")
public class FileNormalController {

    @Resource
    private FileService fileService;
    @RequestMapping("/preview/{id}")
    public ModelAndView preview(@PathVariable("id")Integer id, HttpServletRequest request)
            throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        org.wingstudio.entity.File file=fileService.findFileById(id);
        org.wingstudio.entity.File testFile=new org.wingstudio.entity.File();
        testFile.setClickHit(file.getClickHit()+1);
        testFile.setId(file.getId());
        fileService.updateFile(testFile);
        modelAndView.addObject("file",file);
        modelAndView.setViewName("user/file/file");
        return modelAndView;
    }


    @RequestMapping("/policy")
    public ModelAndView police(@RequestParam(value="page",required=false)String page,
                               HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("fileTypeId",1);
        List<org.wingstudio.entity.File> newsList=fileService.listFile(map);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/file/policy",
                fileService.getTotal(map), Integer.parseInt(page), 10));
        modelAndView.addObject("policyList",newsList);
        modelAndView.setViewName("user/file/policy");


        return modelAndView;
    }

    @RequestMapping("/manage")
    public ModelAndView manage(@RequestParam(value="page",required=false)String page,
                               HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("fileTypeId",2);
        List<org.wingstudio.entity.File> newsList=fileService.listFile(map);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/file/manage",
                fileService.getTotal(map), Integer.parseInt(page), 10));
        modelAndView.addObject("manageList",newsList);
        modelAndView.setViewName("user/file/manage");


        return modelAndView;
    }

    @RequestMapping("/house")
    public ModelAndView house(@RequestParam(value="page",required=false)String page,
                               HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("fileTypeId",3);
        List<org.wingstudio.entity.File> newsList=fileService.listFile(map);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/file/house",
                fileService.getTotal(map), Integer.parseInt(page), 10));
        modelAndView.addObject("houseList",newsList);
        modelAndView.setViewName("user/file/house");


        return modelAndView;
    }
}
